﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using Business;

namespace QuanLyNhaSach
{
    public partial class PhieuNhapSach : Form
    {
        public PhieuNhapSach()
        {
            InitializeComponent();
        }
        private void SetUpDataGridViewPhieuNhapSach()
        {
            dgvPhieuNhapSach.BackgroundColor = Color.White;
            dgvPhieuNhapSach.ColumnCount = 5;
            dgvPhieuNhapSach.Columns[0].Name = "STT";
            dgvPhieuNhapSach.Columns[1].Name = "Mã Sách";
            dgvPhieuNhapSach.Columns[2].Name = "Số Lượng Tồn";
            dgvPhieuNhapSach.Columns[3].Name = "Số Lượng";            
            dgvPhieuNhapSach.Columns[4].Name = "Số Lượng Tồn Mới";



            dgvPhieuNhapSach.Columns[0].ReadOnly = true;


           

            dgvPhieuNhapSach.AllowUserToAddRows = true;
            dgvPhieuNhapSach.AllowUserToDeleteRows = false;
            dgvPhieuNhapSach.MultiSelect = false;
            dgvPhieuNhapSach.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvPhieuNhapSach.DefaultCellStyle.SelectionBackColor = Color.Ivory; //Color.Silver;
            dgvPhieuNhapSach.DefaultCellStyle.SelectionForeColor = Color.Black;


            DataGridViewComboBoxColumn comboboxColumnSach = new DataGridViewComboBoxColumn();
            comboboxColumnSach.HeaderText = "Tên Sách";
            comboboxColumnSach.Name = "Tên Sách";
            comboboxColumnSach.DataSource = SachService.GetAllSach();
            comboboxColumnSach.DisplayMember = SachColumns.TenSach;
            comboboxColumnSach.ValueMember = SachColumns.MaSach;
            
            DataGridViewComboBoxColumn comboboxColumnTheLoai = new DataGridViewComboBoxColumn();
            comboboxColumnTheLoai.HeaderText = "Thể Loại";
            comboboxColumnTheLoai.Name = "Thể Loại";


            comboboxColumnTheLoai.DataSource = TheLoaiService.GetAllTheLoai();
            comboboxColumnTheLoai.DisplayMember = TheLoaiColumns.TenTheLoai;
             comboboxColumnTheLoai.ValueMember = TheLoaiColumns.MaTheLoai;
           // comboboxColumnTheLoai.ValueMember = TheLoaiColumns.TenTheLoai;
           


            DataGridViewComboBoxColumn comboboxColumnTacGia = new DataGridViewComboBoxColumn();
            comboboxColumnTacGia.HeaderText = "Tác Giả";
            comboboxColumnTacGia.Name = "Tác Giả";
            comboboxColumnTacGia.DataSource = TacGiaService.GetAllTacGia();
            comboboxColumnTacGia.DisplayMember = TacGiaColumns.TenTacGia;
             comboboxColumnTacGia.ValueMember = TacGiaColumns.MaTacGia;
            //comboboxColumnTacGia.ValueMember = TacGiaColumns.TenTacGia;



            DataGridViewComboBoxColumn comboboxColumnNhaXuatBan = new DataGridViewComboBoxColumn();
            comboboxColumnNhaXuatBan.HeaderText = "Nhà Xuất Bản";
            comboboxColumnNhaXuatBan.Name = "Nhà Xuất Bản";
            comboboxColumnNhaXuatBan.DataSource = NhaXuatBanService.GetAllNhaXuatBan();
            comboboxColumnNhaXuatBan.DisplayMember = NhaXuatBanColumns.TenNhaXuatBan;
             comboboxColumnNhaXuatBan.ValueMember = NhaXuatBanColumns.MaNhaXuatBan;
            //comboboxColumnNhaXuatBan.ValueMember = NhaXuatBanColumns.TenNhaXuatBan;

            dgvPhieuNhapSach.Columns.Insert(2, comboboxColumnSach);
            dgvPhieuNhapSach.Columns.Insert(3, comboboxColumnTheLoai);
           dgvPhieuNhapSach.Columns.Insert(4, comboboxColumnTacGia);

           dgvPhieuNhapSach.Columns[2].ReadOnly = true;
           dgvPhieuNhapSach.Columns[3].ReadOnly = true;
           dgvPhieuNhapSach.Columns[4].ReadOnly = true;
           dgvPhieuNhapSach.Columns[5].ReadOnly = true;
           dgvPhieuNhapSach.Columns[7].ReadOnly = true;

           

            /*   MessageBox.Show(dgvPhieuNhapSach.Columns[0].Name.ToString()); 
               MessageBox.Show(dgvPhieuNhapSach.Columns[1].Name.ToString()); 
               MessageBox.Show(dgvPhieuNhapSach.Columns[2].Name.ToString());
               MessageBox.Show(dgvPhieuNhapSach.Columns[3].Name.ToString());
               MessageBox.Show(dgvPhieuNhapSach.Columns[4].Name.ToString());
               MessageBox.Show(dgvPhieuNhapSach.Columns[5].Name.ToString());
               MessageBox.Show(dgvPhieuNhapSach.Columns[6].Name.ToString()); */



        }
        private void UpdateSoThuTuDataGridViewTheLoai()
        {
            for (int i = 0; i < dgvPhieuNhapSach.Rows.Count - 1; i++)
            {
                dgvPhieuNhapSach.Rows[i].Cells[0].Value = i + 1;
            }
        }

        private void PhieuNhapSach_Load(object sender, EventArgs e)
        {
            SetUpDataGridViewPhieuNhapSach();
            txtMaPhieuNhap.Text = NumberIdService.NewNumber("PHIEUNHAPSACH").ToString();
        }

        private void dgvPhieuNhapSach_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            
            UpdateSoThuTuDataGridViewTheLoai();
        }

        private void dgvPhieuNhapSach_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
         /*  if (e.ColumnIndex == 1)
            {
                int masach;
                if (int.TryParse(dgvPhieuNhapSach.Rows[e.RowIndex].Cells[1].Value.ToString(), out masach))
                {

                    if (SachService.KiemTraTonTaiMaSach(masach))
                    {
                        SachDTO sach = SachService.GetSachByID(masach);
                        ThamSoDTO thamso = ThamSoService.GetThamSoByID(2);
                        if (sach.SoLuongTon < thamso.GiaTri)
                        {
                            dgvPhieuNhapSach.Rows[e.RowIndex].Cells[2].Value = sach.MaSach;
                            dgvPhieuNhapSach.Rows[e.RowIndex].Cells[3].Value = sach.MaTheLoai;
                            dgvPhieuNhapSach.Rows[e.RowIndex].Cells[4].Value = sach.MaTacGia;
                            dgvPhieuNhapSach.Rows[e.RowIndex].Cells[5].Value = sach.SoLuongTon;
                        }
                        else if (sach.SoLuongTon >= thamso.GiaTri)
                        {
                            MessageBox.Show("Chỉ nhập đầu sách có số lượng tồn ít hơn " + thamso.GiaTri);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Mã sách không tồn tại");
                        MessageBox.Show(dgvPhieuNhapSach.Rows[e.RowIndex].Cells[1].Value.ToString());
                      
                    }
                   
                                                                                                                                     
                }
                else
                {
                    MessageBox.Show("Mã sách phải là số");
                    
                }

            }
            if (e.ColumnIndex == 6)
            {
                int soluong;
                if (int.TryParse(dgvPhieuNhapSach.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), out soluong))
                {
                    int soluongtoncu = int.Parse(dgvPhieuNhapSach.Rows[e.RowIndex].Cells[5].Value.ToString());
                    int soluongtonmoi = soluongtoncu - soluong;
                    dgvPhieuNhapSach.Rows[e.RowIndex].Cells[7].Value = soluongtonmoi;
                }
                else
                {
                    MessageBox.Show("Số lượng phải là số");
                   
                }
            }*/
        }

       

        private void dgvPhieuNhapSach_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
           
            if (e.ColumnIndex == 1)
            {
                if (dgvPhieuNhapSach.Rows[e.RowIndex].Cells[1].Value != null)
                {
                    int masach;
                    if (int.TryParse(dgvPhieuNhapSach.Rows[e.RowIndex].Cells[1].Value.ToString(), out masach))
                    {

                        if (SachService.KiemTraTonTaiMaSach(masach))
                        {
                            SachDTO sach = SachService.GetSachByID(masach);
                            ThamSoDTO thamso = ThamSoService.GetThamSoByID(2);
                            if (sach.SoLuongTon < thamso.GiaTri)
                            {
                                ThamSoDTO soluongtoitieu = ThamSoService.GetThamSoByID(1);
                                dgvPhieuNhapSach.Rows[e.RowIndex].Cells[1].ErrorText = null;
                                dgvPhieuNhapSach.Rows[e.RowIndex].Cells[6].ErrorText = null;
                                dgvPhieuNhapSach.Rows[e.RowIndex].Cells[2].Value = sach.MaSach;
                                dgvPhieuNhapSach.Rows[e.RowIndex].Cells[3].Value = sach.MaTheLoai;
                                dgvPhieuNhapSach.Rows[e.RowIndex].Cells[4].Value = sach.MaTacGia;
                                dgvPhieuNhapSach.Rows[e.RowIndex].Cells[5].Value = sach.SoLuongTon;
                                dgvPhieuNhapSach.Rows[e.RowIndex].Cells[6].Value =soluongtoitieu.GiaTri;
                                dgvPhieuNhapSach.Rows[e.RowIndex].Cells[7].Value = sach.SoLuongTon + soluongtoitieu.GiaTri;
                                dgvPhieuNhapSach.Rows[e.RowIndex].Cells[6].ReadOnly = false;
                               // dgvPhieuNhapSach.AllowUserToAddRows =false;
                                
                            }
                            else if (sach.SoLuongTon >= thamso.GiaTri)
                            {
                               
                                dgvPhieuNhapSach.Rows[e.RowIndex].Cells[1].ErrorText = "Chỉ nhập đầu sách có số lượng tồn ít hơn " + thamso.GiaTri;
                                dgvPhieuNhapSach.Rows[e.RowIndex].Cells[1].Selected = true;
                                dgvPhieuNhapSach.Rows[e.RowIndex].Cells[1].Value = null;
                              
                                dgvPhieuNhapSach.Rows[e.RowIndex].Cells[6].ReadOnly = true;
                             //  dgvPhieuNhapSach.AllowUserToAddRows = false;
                              
                            }
                        }
                        else
                        {
                           
                            dgvPhieuNhapSach.Rows[e.RowIndex].Cells[1].ErrorText = "Mã sách không tồn tại";
                            dgvPhieuNhapSach.Rows[e.RowIndex].Cells[1].Selected = true;
                              // MessageBox.Show(dgvPhieuNhapSach.Rows[e.RowIndex].Cells[1].Value.ToString());
                            dgvPhieuNhapSach.Rows[e.RowIndex].Cells[1].Value = null;

                            dgvPhieuNhapSach.Rows[e.RowIndex].Cells[6].ReadOnly = true;
                           // dgvPhieuNhapSach.AllowUserToAddRows = false;
                            

                        }
                    }
                    else
                    {
                       
                        dgvPhieuNhapSach.Rows[e.RowIndex].Cells[1].ErrorText = "Mã sách phải là số";
                        dgvPhieuNhapSach.Rows[e.RowIndex].Cells[1].Selected = true;
                        dgvPhieuNhapSach.Rows[e.RowIndex].Cells[1].Value = null;
                        dgvPhieuNhapSach.Rows[e.RowIndex].Cells[6].ReadOnly = true;
                      //  dgvPhieuNhapSach.AllowUserToAddRows = false;
                     

                    }
                }
                else
                {
                  
                    //dgvPhieuNhapSach.AllowUserToAddRows = false;
                    
                }

            }
            if (e.ColumnIndex == 6)
            {
                if (dgvPhieuNhapSach.Rows[e.RowIndex].Cells[6].Value != null)
                {                                       
                    if (dgvPhieuNhapSach.Rows[e.RowIndex].Cells[1].Value != null)
                    {
                       
                        int soluong;
                        if (int.TryParse(dgvPhieuNhapSach.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), out soluong))
                        {
                            ThamSoDTO tham = ThamSoService.GetThamSoByID(1);
                            if (soluong >= tham.GiaTri)
                            {
                                dgvPhieuNhapSach.Rows[e.RowIndex].Cells[6].ErrorText = null;
                                int soluongtoncu;
                                if (int.TryParse(dgvPhieuNhapSach.Rows[e.RowIndex].Cells[5].Value.ToString(), out soluongtoncu))
                                {
                                    int soluongtonmoi = soluongtoncu + soluong;
                                    dgvPhieuNhapSach.Rows[e.RowIndex].Cells[7].Value = soluongtonmoi;
                                    //  dgvPhieuNhapSach.AllowUserToAddRows = true;
                                }
                            }
                            else
                            {
                                dgvPhieuNhapSach.Rows[e.RowIndex].Cells[6].ErrorText = "Số lượng nhập ít nhất là " + tham.GiaTri.ToString();
                            }

                        }
                        else
                        {
                            //  dgvPhieuNhapSach.AllowUserToAddRows = false;
                            dgvPhieuNhapSach.Rows[e.RowIndex].Cells[6].ErrorText = "Số lượng phải là số";
                            dgvPhieuNhapSach.Rows[e.RowIndex].Cells[6].Selected = true;
                            dgvPhieuNhapSach.Rows[e.RowIndex].Cells[6].Value = null;


                        }
                    }
                    else
                    {
                        dgvPhieuNhapSach.Rows[e.RowIndex].Cells[6].ErrorText ="Chưa Nhập Mã Sách";
                    }
                }
                else
                {
                   // dgvPhieuNhapSach.AllowUserToAddRows = false;
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dgvPhieuNhapSach.SelectedRows[0].IsNewRow==false)
            {
                dgvPhieuNhapSach.Rows.RemoveAt(dgvPhieuNhapSach.SelectedRows[0].Index);
                UpdateSoThuTuDataGridViewTheLoai();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
           // bool kq =false;
            PhieuNhapSachDTO phieunhapsachDTO = new PhieuNhapSachDTO();
            phieunhapsachDTO.MaPhieuNhap =int.Parse( txtMaPhieuNhap.Text);
            phieunhapsachDTO.NgayNhap = DateTime.Parse(dtpNgayNhap.Text);
           if (PhieuNhapSachService.Insert(phieunhapsachDTO))
            {
                NumberIdService.Update("PHIEUNHAPSACH");
                if (dgvPhieuNhapSach.Rows.Count - 1 > 0)
                {
                    for (int i = 0; i < dgvPhieuNhapSach.Rows.Count - 1; i++)
                    {
                       
                        ChiTietPhieuNhapDTO chitietphieunhapDTO = new ChiTietPhieuNhapDTO();
                        chitietphieunhapDTO.MaPhieuNhap = phieunhapsachDTO.MaPhieuNhap;
                        chitietphieunhapDTO.MaSach =int.Parse( dgvPhieuNhapSach.Rows[i].Cells[1].Value.ToString());
                        chitietphieunhapDTO.SoLuongNhap = int.Parse(dgvPhieuNhapSach.Rows[i].Cells[6].Value.ToString());
                      
                        SachDTO sachDTO = new SachDTO();
                        sachDTO = SachService.GetSachByID(chitietphieunhapDTO.MaSach);
                        sachDTO.SoLuongTon=sachDTO.SoLuongTon+chitietphieunhapDTO.SoLuongNhap;
                        MessageBox.Show(sachDTO.SoLuongTon.ToString());
                        MessageBox.Show(sachDTO.MaSach.ToString());
                        MessageBox.Show(sachDTO.MaTacGia.ToString());
                        MessageBox.Show(sachDTO.MaTheLoai.ToString());
                        MessageBox.Show(sachDTO.MaNhaXuatBan.ToString());

                        if (ChiTietPhieuNhapService.Insert(chitietphieunhapDTO)&& SachService.Update(sachDTO))
                        {
                            MessageBox.Show("da cap nhat");
                        }
                        else
                        {
                            MessageBox.Show("chua cap nhat");
                        }
                       

                      /*  if (ChiTietPhieuNhapService.Insert(chitietphieunhapDTO) && SachService.Update(sachDTO))
                        {
                            NumberIdService.Update("CHITIETPHIEUNHAP");
                            kq =true;
                        }*/
                        
                    }
                }
            }
         /*   if (kq == false)
            {
                MessageBox.Show("Lưu dữ liệu thất bại", "Thông báo");
            }
            else
            {
                MessageBox.Show("Đã lưu dữ liệu thành công", "Thông báo");
            }*/
        }
      

        
        private void AnHienButtonSave()
        {
            if (dgvPhieuNhapSach.Rows.Count - 1 > 0)
            {
                btnSave.Enabled = true;
            }
            else
                btnSave.Enabled = false;
        }
        private bool KiemTraDuLieu( )
        {
            bool kq = true;
           
            for (int i = 0; i < dgvPhieuNhapSach.Rows.Count - 1; i++)
            {
                if (dgvPhieuNhapSach.Rows[i].Cells[1].Value != null)
                {
                    int masach;
                    if (int.TryParse(dgvPhieuNhapSach.Rows[i].Cells[1].Value.ToString(), out masach))
                    {
                        if (SachService.KiemTraTonTaiMaSach(masach))
                        {
                            if (dgvPhieuNhapSach.Rows[i].Cells[6].Value != null)
                            {
                                int soluong;
                                if (int.TryParse(dgvPhieuNhapSach.Rows[i].Cells[6].Value.ToString(), out soluong))
                                {
                                    ThamSoDTO soluongtoithieu = ThamSoService.GetThamSoByID(1);
                                    if (soluong < soluongtoithieu.GiaTri)
                                    {
                                        kq = false;
                                    }
                                    //  return kq;
                                  /*  else
                                    {
                                        kq = false;
                                    }*/
                                }
                                else
                                {
                                    kq = false;
                                }

                            }
                            else
                            {
                                kq = false;
                            }
                        }
                        else
                        {
                            kq = false;
                        }
                    }
                    else
                    {
                        kq = false;
                    }
                }
                else
                {
                   
                    kq = false;
                }
            }
            
            return kq;
        }

        private void dgvPhieuNhapSach_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            AnHienButtonSave();
        }

       
        private void dgvPhieuNhapSach_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (dgvPhieuNhapSach.SelectedRows[0].IsNewRow == false)
            {
                if (!KiemTraDuLieu())
                {
                    
                    btnSave.Enabled = false;
                }
                else
                {
                  
                    btnSave.Enabled = true;
                }
            }
           
        }
                                                      
    }
}
